﻿package linearList;

public class SeqList<T> implements List<T> {
    private Object[] elements;
    private int length;

    public SeqList() {
        this.elements = new Object[10];
        this.length = 0;
    }

    public SeqList(T[] data) {
        this.elements = new Object[data.length];
        for(int i = 0; i < data.length; i++) {
            this.elements[i] = data[i];
        }

        this.length = data.length;
    }

    public static void main(String[] args) {
        System.out.println("--测试初始化--");
        Integer[] data = {25, 54,15,25, 15, 4, 5, 9, 45, 65};
        SeqList<Integer> seqList = new SeqList<Integer>(data);
        System.out.println(seqList);

        System.out.println("--测试insert(3, 44)--");
        seqList.insert(5, 48);
        System.out.println(seqList);

        System.out.println("--测试insert 10个元素--");
        for (int i = 20; i <= 30; i++) {
            seqList.insert(i);
        }
        System.out.println(seqList);

        System.out.println("--测试remove index为10的元素--");
        seqList.removeKey(9);
        System.out.println(seqList);

        System.out.println("--测试获取 index为5的元素--");
        System.out.println(seqList.get(5));

        System.out.println("--测试设置 index为5的元素为100--");
        seqList.set(5, 100);
        System.out.println(seqList);

        System.out.println("--测试查找元素等于100的索引--");
        System.out.println(seqList.search(100));

        System.out.println("--测试remove by key--");
        seqList.removeByKey(new Integer(100));
        System.out.println(seqList);

        System.out.println("--输出元素之和--")
        System.out.println(seqList.sum());
    //逆置
        for(int i=0;i<elements.length;i++)
            seqList.add(elements[i], i+1);
        System.out.println("逆置前顺序表La中的数据元素为：");
        seqList.nextOrder();
        SqInversion(seqList);
        System.out.println("逆置后顺序表La中的数据元素为：");
        seqList.nextOrder();
    }

}

    @Override
    public boolean isEmpty() {
        return this.n==0;
    }

    @Override
    public int size() {  //返回元素个数
        return length;
    }

    @Override
    public T get(int i) {
        if(i>=0&&i<this.n)
            return (T)this.elements[i];
        return null;
    }

    @Override
    public void set(int i, T x) {
	if(x==null)
	     throw new NullPointerException("x==null");
	  if(i>=0&&i<this.n)
		this.elements[i]=x;
 	else
           throw new java.lang.IndexOutOfBoundsException(i+"");

    }

    @Override
    public int insert(int i, T x)
    {
      if(x==null)
          return -1;
      if(i<0)
          i=0;
      if(i>this.n)
          i=this.n;
      Object[] source=this.elements;
      if(this.n==elements.length){
          this.elements =new Object[source.length*2];
          for(j=0;j<i;j++)
              this.elements[j]=source[j];
      }
      for(int j=this.n-1;j>=i;j--)
          this.elements[i]=x;
      this.n++;
      return i;
    }

    @Override
    public int insert(T x) {
       return this.insert(this.n,x);//调用insert(i,x)方法
    }

    @Override
    public T remove(int i)
    {  //删除
        if(i>=0&&i<this.n)
        {
            T x=(T)this.elements[i];
            for(int j=i;j<this.n-1;j++)
                this.elements[j]=this.element[j+1];
            this.elements[this.n-1]=null;
            this.n--;
            return x;
        }
        return null;
    }

    @Override
    public void clear()
    {
        for (int i = 0; i < this.length; i++) {
            this.elements[i] = null;
        }  //设置长度为0，未释放数组空间
    }

    @Override
    public void sum() //求和
    {
        int s=0;
        for (int i = 0; i < this.length; i++)
        {
           s+=this.elements[i];
        }
        return s;
    }


    @Override
    public int search(T key)
    {
       for(int i=0;i<this.n;i++){
           if(key.equals(this.element[i]))
               return i;
           return -1;   //空表或未找到时
       }
    }

    @Override
    public T removeByKey(T key) {
        {
            int i = 0,j = 0;

            while(i < elements.length)
            {
                if(elements[i] == val)
                    j++;
                else
                    elements[i-j] = elemtents[i];
                i++;
            }
            return len-j;
        }
    }

    // 在顺序表中查找元素obj出现的位置
    public int find(T obj) {
        if (isEmpty()) {
            System.out.println("顺序表为空");
            return -1;
        } else {
            for (int i = 0; i < length; i++)
                if (listArray[i].equals(obj))
                    return i + 1;
            return -1;
        }
    }
    // 获取顺序表第pos个位置的元素
    public T value(int pos) {
        if (isEmpty()) {
            System.out.println("顺序表为空");
            return null;
        } else {
            if (pos < 1 || pos > length) {
                System.out.println("pos值不合法");
                return null;
            }
            return elements[pos - 1];
        }
    }
    // 修改顺序表第pos个位置的元素
    public boolean modify(T obj, int pos) {
        if (isEmpty()) {
            System.out.println("顺序表为空");
            return false;
        } else {
            if (pos < 1 || pos > length) {
                System.out.println("pos值不合法");
                return false;
            }
            listArray[pos - 1] = obj;
            return true;
        }
    }
    // 正序输出顺序表中所有元素
    public void nextOrder() {
        for (int i = 0; i < length; i++)
            System.out.print(listArray[i] + " ");
        System.out.println();
    }
    //就地逆置算法方法2
    public void getInversionList(){
        if(isEmpty()){
            //判断是否为空
            System.out.println("顺序表为空");
        }else{
            //临时变量
            T temp;
            for (int i = 0; i < Size()/2; i++) {
                temp=(T) elements[i];
                elements[i]=elements[Size()-i-1];
                elements[Size()-i-1] =temp;
            }
        }
    }



}
